﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
public partial class Admin_MembershipProvider_Access_MemberRoles : System.Web.UI.Page
{
    private bool createRoleSuccess = true;

    private void Page_PreRender()
    {
        // Create a DataTable and define its columns
        DataTable RoleList = new DataTable();
        RoleList.Columns.Add("Role Name");
        RoleList.Columns.Add("User Count");

        string[] allRoles = System.Web.Security.Roles.GetAllRoles();


        // Get the list of roles in the system and how many users belong to each role
        foreach (string roleName in allRoles)
        {
            int numberOfUsersInRole = System.Web.Security.Roles.GetUsersInRole(roleName).Length;
            string[] roleRow = { roleName, numberOfUsersInRole.ToString() };
            RoleList.Rows.Add(roleRow);
        }

        // Bind the DataTable to the GridView
        UserRoles.DataSource = RoleList;
        UserRoles.DataBind();

        if (createRoleSuccess)
        {
            // Clears form field after a role was successfully added. Alternative to redirect technique I often use.
            NewRole.Text = "";
        }
    }
    private void AddRole(object sender, EventArgs e)
    {
        try
        {
            System.Web.Security.Roles.CreateRole(NewRole.Text);
            ConfirmationMessage.InnerText = "The new role was added.";
            createRoleSuccess = true;
        }
        catch (Exception ex)
        {
            ConfirmationMessage.InnerText = ex.Message;
            createRoleSuccess = false;
        }
    }

    private void DeleteRole(object sender, CommandEventArgs e)
    {
        try
        {
            System.Web.Security.Roles.DeleteRole(e.CommandArgument.ToString());
            ConfirmationMessage.InnerText = "Role '" + e.CommandArgument.ToString() + "' was deleted.";
        }
        catch (Exception ex)
        {
            ConfirmationMessage.InnerText = ex.Message;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void UserRoles_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        if (e.CommandName == "DeleteRole")
        {
            try
            {

                System.Web.Security.Roles.DeleteRole(e.CommandArgument.ToString());
                ConfirmationMessage.InnerText = "Role '" + e.CommandArgument.ToString() + "' was deleted.";
            }
            catch (Exception ex)
            {
                ConfirmationMessage.InnerText = ex.Message;
            }
        }


    }

    protected void btnAddRole_Click(object sender, EventArgs e)
    {
        try
        {
            System.Web.Security.Roles.CreateRole(NewRole.Text);
            ConfirmationMessage.InnerText = "The new role was added.";
            createRoleSuccess = true;
        }
        catch (Exception ex)
        {
            ConfirmationMessage.InnerText = ex.Message;
            createRoleSuccess = false;
        }
    }
}
